import {
  updateUserInfo
} from '../../utils/api'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    zfb: '',
    zfbName: ''
  },

  onLoad: function (option) {
    var that = this
    const eventChannel = this.getOpenerEventChannel();
    eventChannel.on('updateZfb', function(data) {
      that.setData({
        zfb: data.data.zfb,
        zfbName: data.data.zfbName
      })
    })
  },

  /**
   * 修改用户信息
   */
  updateUserInfo() {
    if(!this.data.zfbName||!this.data.zfb){
      wx.showToast({
        title: '信息不完整',
        icon: 'error'
      })
      return false
    }

    updateUserInfo({
      zfb: this.data.zfb,
      zfbName: this.data.zfbName
    }).then(res => {
      if (res.data > 0) {
        let pages = getCurrentPages()
        let prevPage = pages[pages.length-2]
        prevPage.setData({
          'userInfo.zfb': this.data.zfb,
          'userInfo.zfbName': this.data.zfbName
        })
        wx.navigateBack({
          delta: 1
        })
      }
    })
  },
  /**
   * 支付宝
   */
  onZfb(e){
    this.setData({
      zfb: e.detail.value
    })
  },
   /**
    * 姓名
    */
  onName(e){
    this.setData({
      zfbName: e.detail.value
    })
  }
})